﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/17 15:19:02
 * ************************************************************/
namespace Mozlite.FileStorages.Editors.KindEditor
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;

    /// <summary>
    /// 浏览文件类型。
    /// </summary>
    public class BrowserDir
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.FileStorages.Editors.KindEditor.BrowserDir"/>。
        /// </summary>
        /// <param name="folder">文件夹实例。</param>
        /// <param name="order">排序。</param>
        /// <param name="type">文件类型。</param>
        public BrowserDir(UserFolder folder, StorageSortOrder order, FileType type)
        {
            this.total_count = folder.Children == null ? 0 : folder.Children.Count;
            this.file_list = new List<BrowserItem>();
            if (folder.Children != null)
            {
                folder.Children.Sort(new UserFolderNameSorter());
                foreach (var child in folder.Children)
                {
                    this.file_list.Add(new BrowserItem { 
                        is_dir = true,
                        datetime = child.LastModified.ToLocalDateTimeString(),
                        filename = child.FolderName,
                        filesize = child.ContentLength,
                        filetype = "",
                        has_file = !child.IsEmpty,
                        is_photo = false,
                        fid = child.FolderID
                    });
                }
            }
            if (folder.Files != null) {
                this.total_count += folder.Files.Count;
                switch (order)
                { 
                    case StorageSortOrder.Name:
                        folder.Files.Sort(new UserFileNameSorter());
                        break;
                    case StorageSortOrder.Size:
                        folder.Files.Sort(new UserFileSizeSorter());
                        break;
                    case StorageSortOrder.Type:
                        folder.Files.Sort(new UserFileTypeSorter());
                        break;
                }
                foreach (var file in folder.Files) {
                    if ((file.FileType & type) != type)
                        continue;
                    this.file_list.Add(new BrowserItem
                    {
                        is_dir = false,
                        datetime = file.LastModified.ToLocalDateTimeString(),
                        filename = file.FullName,
                        filesize = file.ContentLength,
                        filetype = file.Extension,
                        has_file = false,
                        is_photo = file.FileType == FileType.Image,
                        fid = file.FileID
                    });
                }
            }

        }
        /// <summary>
        /// 上一级文件夹路径。
        /// </summary>
        public string moveup_dir_path { get; set; }
        /// <summary>
        /// 当前文件夹路径。
        /// </summary>
        public string current_dir_path { get; set; }
        /// <summary>
        /// 当前地址。
        /// </summary>
        public string current_url { get; set; }
        /// <summary>
        /// 总共数量。
        /// </summary>
        public int total_count { get; set; }
        /// <summary>
        /// 文件列表。
        /// </summary>
        public List<BrowserItem> file_list { get; set; }
    }
}